﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ProjectHT.YKDGVB2012.Core.Datacontract;
using Telerik.Web.UI;

namespace ProjectHT.YKDGVB2012.Core.Webpage
{
    public class DanhsachGopy : LayoutsPageBase
    {
        private IComment _comment;
        private IMainList _main;
        public DanhsachGopy()
        {
            var services = new CommentServices(SPContext.Current);
            _comment = services;
            var mainservice = new MainServices(SPContext.Current);
            _main = mainservice;


        }
        protected RadGrid _raddtGrid;
        protected override bool AllowAnonymousAccess
        {
            get { return false; }
        }

        protected override void OnInit(EventArgs e)
        {
            var gr = new GridGroupByExpression();
            var grbyf = new GridGroupByField { FieldName = "IDMain", HeaderText = "Dự thảo văn bản:" };
            gr.GroupByFields.Add(grbyf);
            gr.SelectFields.Add(grbyf);

            _raddtGrid.MasterTableView.GroupByExpressions.Add(gr);
            _raddtGrid.Culture = new CultureInfo("vi-VN");
            _raddtGrid.NeedDataSource += new GridNeedDataSourceEventHandler(_raddtGrid_NeedDataSource);

            base.OnInit(e);
        }

        protected void _raddtGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            _raddtGrid.DataSource = Data();

        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }


        private List<Comments> Data()
        {
            return _comment.GetAllComment();

        }

        protected string GetAttach(string id)
        {
            var str = string.Empty;
            if (Data() != null)
            {

                if (String.IsNullOrEmpty(id)) return string.Empty;
                var item = _comment.GetItem(Convert.ToInt32(id));
                var acttach = item.Attachments.Count;
                if (acttach > 0)
                {

                    for (int i = 0; i < acttach; i++)
                    {
                        var filepath = "/lists/" + Common.MainList.MAINLIST + "/Attachments/" + item.ID + "/" +
                                       item.Attachments[i];
                        str += "<a href='" + filepath + "'>" + item.Attachments[i] + "</a><br />";
                    }
                }
            }
            return str;

        }

        protected string GetTitle(string id)
        {
            var str = string.Empty;
            if (Data() != null)
            {
                if (String.IsNullOrEmpty(id)) return string.Empty;
                var item = _main.GetbyId(Convert.ToInt32(id));
                if(item!=null)
                    str = item.Title;
            }
            return str;
        }
    }
}
